// Javascript code
/*
 * author: ceet --- ceet@vip.qq.com
 * date: 2019.11.18
 * lastUpdateDate: 2019.11.18
 * description: 豆豆PC官网通用的函数封装
 */
function css ( obj, attr, value ) {
	if ( arguments.length == 2 ) {
		if ( attr == 'scale' || 
			attr == 'rotate' || 
			attr == 'rotateX' || 
			attr == 'rotateY' || 
			attr == 'scaleX' || 
			attr == 'scaleY' || 
			attr == 'translateY' || 
			attr == 'translateX' || 
			attr == 'translateZ') {
			if ( !obj.$Transform ) {
				obj.$Transform = {};
			}
			switch ( attr ) {
				case 'scale':
				case 'scaleX':
				case 'scaleY':
					return typeof ( obj.$Transform[attr] ) == 'number' ? obj.$Transform[attr] : 100;
					break;
				case 'translateY':
				case 'translateX':
				case 'translateZ':
					return obj.$Transform[attr] ? obj.$Transform[attr] : 0;
					break;
				default:
					return obj.$Transform[attr] ? obj.$Transform[attr] : 0;
			}
		}
		var sCur = obj.currentStyle ? obj.currentStyle[attr] : document.defaultView.getComputedStyle ( obj, false )[attr];
		if ( attr == 'opacity' ) {
			return Math.round ( ( parseFloat ( sCur ) * 100 ) );
		} else {
			return parseInt ( sCur );
		}
	} else if ( arguments.length == 3 ) {
		switch ( attr ) {
			case 'scale':
			case 'scaleX':
			case 'scaleY':
			case 'rotate':
			case 'rotateX':
			case 'rotateY':
			case 'translateY':
			case 'translateX':
			case 'translateZ':
				setCss3 ( obj, attr, value );
				break;
			case 'width':
			case 'height':
			case 'paddingLeft':
			case 'paddingTop':
			case 'paddingRight':
			case 'paddingBottom':
				value = Math.max ( value, 0 );
			case 'left':
			case 'top':
			case 'marginLeft':
			case 'marginTop':
			case 'marginRight':
			case 'marginBottom':
				if ( typeof value == "string" ) {
					obj.style[attr] = value;
				} else {
					obj.style[attr] = value + 'px';
				}
				break;
			case 'opacity':
				obj.style.filter = "alpha(opacity:" + value + ")";
				obj.style.opacity = value / 100;
				break;
			default:
				obj.style[attr] = value;
		}
	}
	return function ( attr_in, value_in ) {
		css ( obj, attr_in, value_in );
	};
}

function setCss3 ( obj, attr, value ) {
	var sTr = "";
	var sVal = "";
	var arr = ["Webkit", "Moz", "O", "ms", ""];
	if ( !obj["$Transform"] ) {
		obj["$Transform"] = {};
	}
	obj["$Transform"][attr] = parseInt ( value );
	for ( sTr in obj["$Transform"] ) {
		switch ( sTr ) {
			case 'scale':
			case 'scaleX':
			case 'scaleY':
				sVal += sTr + "(" + ( obj["$Transform"][sTr] / 100 ) + ") ";
				break;
			case 'rotate':
			case 'rotateX':
			case 'rotateY':
				sVal += sTr + "(" + ( obj["$Transform"][sTr] ) + "deg) ";
				break;
			case 'translateY':
			case 'translateX':
			case 'translateZ':
				sVal += sTr + "(" + ( obj["$Transform"][sTr] ) + "px) ";
				break;
		}
	}
	for ( var i = 0; i < arr.length; i++ ) {
		obj.style[arr[i] + "Transform"] = sVal;
	}
}

// 阻止冒泡
function stopBubble ( ev ) {
	// FF、Chrome浏览器
	if ( ev ) {
		ev.stopPropagation ();
	// IE浏览器
	} else {
		window.event.cancelBubble = true;
	}
}

// 添加事件IE兼容写法
function myAddEvent ( obj, sEvent, fn ) {
	if ( obj.attachEvent ) {
		obj.attachEvent ( 'on' + sEvent, fn );
	} else {
		obj.addEventListener ( sEvent, fn, false );
	}
}

// 获取鼠标的位置信息
function mousePosition ( ev ) {
	let scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
	let clientLeft = document.documentElement.clientLeft || document.body.clientLeft;
	let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
	let clientTop = document.documentElement.clientTop || document.body.clientTop;
	
	// FF，Chrome等浏览器
     if( ev.pageX || ev.pageY ) {  
        // 返回一个多值类型
    	return {
			x: ev.pageX,
			y: ev.pageY,
		};
     } else {
		// IE浏览器
       	return {  
        	x: ev.clientX + scrollLeft - clientLeft,
     		y: ev.clientY + scrollTop - clientTop
		};
	}  
}

// 获取可视区的宽
function viewWidth ()  {
	return document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth;
}

// 获取可视区的高
function viewHeight () {
	return document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight;
}

// 获取非行间的样式
function getStyle ( obj, attr ) {
	if ( obj.currentStyle ) {
		return obj.currentStyle[attr];
	} else {
		return getComputedStyle ( obj, false )[attr];
	}
}

// 获取当前的时间戳
function now () {
	return new Date ().getTime ();
}

// 深拷贝一个对象
function copy ( obj ) {
    var o = {};
    for ( let i in obj ) {
      o[i] = 0;
    }
    return o;
}

// 图片预加载
function preloadImgs ( arr, fnSucc, fnFaild, fnProgress ) {
	let loaded = 0;
	for ( let i = 0; i < arr.length; i++ ) {
		let oImg = new Image ();
		oImg.onload = function () {
			loaded++;
			fnProgress && fnProgress ( 100 * loaded / arr.length );
			if ( loaded == arr.length ) fnSucc && fnSucc ();
			this.onload = this.onerror = null;
			this.src = '';
		};
		
		oImg.onerror = function () {
			fnFaild && fnFaild ( this.src );
			fnFaild = fnSucc = fnProgress = null;
		};
		
		oImg.src = arr[i];
	}
}

/** transitionEnd and animationEnd start */
( function ( root, factory ) {
	if ( typeof define === 'function' ) {
		define ( factory );
	} else if ( typeof exports === 'object' ) {
		module.exports = factory;
	} else {
		root.kaiyouEnd = factory ();
	}
}) ( this, function () {
	let kaiyouEnd = {},
		body = document.body || document.documentElement,
		style = body.style, 
		transition = 'transition',
		transitionEnd,
		animationEnd,
		vendorPrefix; 
	transition = transition.charAt ( 0 ).toUpperCase () + transition.substr ( 1 );
	
	// 鐜板湪鐨刼pera涔熸槸webkit
	vendorPrefix = ( function () {
		let i = 0, vendor = ['Moz', 'Webkit', 'Khtml', 'O', 'ms'];
		while ( i < vendor.length ) {
			if ( typeof style[vendor[i] + transition] === "string" ) {
			  return vendor[i];
			}
			i++;
		}
		return false;
	}) ();

	transitionEnd = ( function () {
		let transEndEventNames = {
		  WebkitTransition : 'webkitTransitionEnd',
		  MozTransition    : 'transitionend',
		  OTransition      : 'oTransitionEnd otransitionend',
		  transition       : 'transitionend'
		};
		
		for ( let name in transEndEventNames ) {
			if ( typeof style[name] === "string" ) {
				return transEndEventNames[name];
			}
		}
	}) ();

	animationEnd = ( function () {
		let animEndEventNames = {
		  WebkitAnimation : 'webkitAnimationEnd',
		  animation       : 'animationend'
		};
		
		for ( let name in animEndEventNames ) {
			if ( typeof style[name] === "string" ) {
				return animEndEventNames[name];
			}
		}
	}) ();
	
	kaiyouEnd.addTranEvent = function ( elem, fn, duration ) {
		let called = false;
		let fncallback = function () {
			if ( !called ) {
				fn ();
				called = true;
			}
		};
		
		function hand () {
			EventUtil.addHandler ( elem, transitionEnd, function () {
				EventUtil.removeHandler ( elem, transitionEnd, function () {
					arguments.callee;	
				});
				fncallback ();
			});
		}
		setTimeout ( hand, duration );
	};
	
	kaiyouEnd.addAnimEvent = function ( elem, fn ) {
		EventUtil.addHandler ( elem, animationEnd, fn );
	};

	kaiyouEnd.removeAnimEvent = function ( elem, fn ) {
		EventUtil.removeHandler ( elem, animationEnd, fn );
	};

	kaiyouEnd.setStyleAttribute = function ( elem, val ) {
		if ( Object.prototype.toString.call ( val ) === '[object Object]' ) {
			for ( let name in val ) {
				if ( /^transition|animation|transform/.test ( name ) ) {
					let styleName = name.charAt ( 0 ).toUpperCase () + name.substr ( 1 );
					elem.style[vendorPrefix + styleName] = val[name];
				} else {
					elem.style[name] = val[name];
				}
			}
		}
	};
	
	kaiyouEnd.transitionEnd = transitionEnd;
	kaiyouEnd.vendorPrefix = vendorPrefix;
	kaiyouEnd.animationEnd = animationEnd;
	return kaiyouEnd;
});

// 事件的封装
let EventUtil = {
	addHandler: function ( element, type, handler ) {
		if ( element.addEventListener ) {
			element.addEventListener ( type, handler, false );
		} else if ( element.attachEvent ) {
			element.attachEvent ( 'on' + type, handler );
		} else {
			element['on' + type] = handler;
		}
	},
	removeHandler: function ( element, type, handler ) {
		if ( element.removeEventListener ) {
			element.removeEventListener ( type, handler, false );
		} else if ( element.detachEvent ) {
			element.detachEvent ( 'on' + type, handler );
		} else {
			element['on' + type] = null;
		}
	}
};
/** transitionEnd and animationEnd end */